package me.samlss.lighter_demo.slice;

import me.samlss.lighter.Lighter;
import me.samlss.lighter.parameter.Direction;
import me.samlss.lighter.parameter.LighterParameter;
import me.samlss.lighter.parameter.MarginOffset;
import me.samlss.lighter.shape.RectShape;
import me.samlss.lighter.util.LogUtil;
import me.samlss.lighter.util.PixelUtil;
import me.samlss.lighter_demo.LighterHelper;
import me.samlss.lighter_demo.ResourceTable;
import me.samlss.lighter_demo.callback.OnItemClickListener;
import me.samlss.lighter_demo.tools.Toast;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.app.Context;

import java.util.ArrayList;
import java.util.List;
/**
 * @author: zhu_zhonglin
 * @time: 2021/4/25
 */
public class GridViewAbilitySlice extends AbilitySlice {
    DependentLayout grid_root;
    private Image back_iv;
    Text title_tv;
    private ListContainer gridView;
    private Adapter adapter;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_grid_view);
        initView();
    }

    private void initView() {
        grid_root = (DependentLayout) findComponentById(ResourceTable.Id_grid_root);
        title_tv = (Text) findComponentById(ResourceTable.Id_title_tv);
        title_tv.setText("GridView");
        back_iv = (Image) findComponentById(ResourceTable.Id_back_iv);
        back_iv.setClickedListener(component -> getAbility().terminateAbility());
        gridView = (ListContainer) findComponentById(ResourceTable.Id_gridview);
        adapter = new Adapter(getContext());
        adapter.setItemCount(3);
        adapter.setDataList(LighterHelper.sPictureList);
        adapter.setListener(new OnItemClickListener() {
            @Override
            public void onItemClick(Component view, int position) {
                LogUtil.loge("===position:" + position);
                showGuide(view);
            }
        });
        gridView.setItemProvider(adapter);
    }

    private void showGuide(Component highlightedView) {
        if (highlightedView == null) {
            return;
        }

        RectShape rectShape = new RectShape();
        rectShape.setPaint(LighterHelper.getDashPaint());

        Lighter.with(grid_root)
                .setBackgroundColor(0x000000, 0.5f)
                .addHighlight(new LighterParameter.Builder()
                        .setHighlightedView(highlightedView)
                        .setAutoVerticalFixed(true)
                        .setTipLayoutId(ResourceTable.Layout_layout_tip_6)
                        .setTipViewRelativeDirection(Direction.TOP)
                        .setLighterShape(rectShape)
                        .setShapeXOffset(10)
                        .setShapeYOffset(10)
                        .setTipViewRelativeOffset(new MarginOffset(0, 0, 0, 20))
                        .build())
                .show();
    }

    @Override
    public void onActive() {
        super.onActive();
        Toast.show(getContext(), "Click to show highlight(点击显示高亮)~");
    }

    public  static class Adapter extends BaseItemProvider {
        private Context mContext;
        private LayoutScatter mLayoutScatter;
        private OnItemClickListener listener;
        private int itemCount;
        private List<List<Integer>> mDataList;

        public Adapter(Context context) {
            this.mContext = context;
            this.mLayoutScatter = LayoutScatter.getInstance(context);
        }

        /**
         * 设置显示数据
         * @param dataList dataList
         * @throws RuntimeException
         */
        public void setDataList(List<Integer> dataList) {
            if (itemCount == 0) {
                throw new RuntimeException("请先设置itemCount的值");
            }
            List<List<Integer>> tempList = new ArrayList<>();
            List<Integer> itemList = new ArrayList<>();
            for (int i = 0; i < dataList.size(); i++) {
                if (i % itemCount == 0) {
                    itemList = new ArrayList<>();
                }
                itemList.add(dataList.get(i));
                if (i % itemCount == itemCount - 1 || (i % itemCount != itemCount - 1 && i == dataList.size() - 1)) {
                    tempList.add(itemList);
                }
            }
            this.mDataList = tempList;
            LogUtil.loge("===list:" + this.mDataList.toString());
            notifyDataChanged();
        }

        /**
         * setItemCount
         * @param itemCount itemCount
         */
        public void setItemCount(int itemCount) {
            this.itemCount = itemCount;
        }

        /**
         * 设置点击事件
         * @param listener listener
         */
        public void setListener(OnItemClickListener listener) {
            this.listener = listener;
        }

        @Override
        public int getCount() {
            return mDataList.size();
        }

        @Override
        public Object getItem(int i) {
            return mDataList.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public Component getComponent(int position, Component component, ComponentContainer componentContainer) {
            ViewHolder holder;
            if (component == null) {
                holder = new ViewHolder();
                component = mLayoutScatter.parse(ResourceTable.Layout_grid_item_list, componentContainer, false);
                holder.item_root = (DirectionalLayout) component.findComponentById(ResourceTable.Id_item_grid_root);
                component.setTag(holder);
            } else {
                holder = (ViewHolder) component.getTag();
            }
            List<Integer> itemList = mDataList.get(position);

            for (int i = 0; i < itemList.size(); i++) {
                Image image = new Image(mContext);
                image.setScaleMode(Image.ScaleMode.CLIP_CENTER);
                int width = (PixelUtil.screenWidth() - (itemCount - 1) * 10) / itemCount;
                DependentLayout.LayoutConfig itemConfig = new DependentLayout.LayoutConfig(width, (int) PixelUtil.fp2px(105));
                itemConfig.setMarginLeft(i % itemCount == 0 ? 0 : 10);
                itemConfig.setMarginTop(position == 0 ? 0 : 10);
                image.setLayoutConfig(itemConfig);

                image.setPixelMap(itemList.get(i));
                holder.item_root.addComponent(image);
                int finalI = i;
                image.setClickedListener((v) -> {
                    if (listener != null) {
                        listener.onItemClick(image, position * itemCount + finalI);
                    }
                });
            }
            return component;
        }
    }

    static class ViewHolder {
        DirectionalLayout item_root;
    }

}
